import pymysql
import time
from aip import AipOcr


# 数据库模块
class sqlmood(object):

    def __init__(self):
        self.mydb = pymysql.connect('47.110.150.123', 'boss', 'Gem1234@', 'medo')
        self.mycursor = self.mydb.cursor()

    def datas(self,plate_number,car_color):
        sql = "SELECT * FROM datas WHERE car_number = '{}' AND car_color = '{}'".format(plate_number,car_color)
        self.mycursor.execute(sql)
        return self.mycursor.fetchall()
    def inset(self, plate_number, colors, arrive_time):
        sql = "INSERT INTO parking_record(`number`,colors,arrive_time) VALUES ('{}','{}','{}')".format(
            plate_number, colors, arrive_time)
        self.mycursor.execute(sql)
        self.mydb.commit()

    def update(self, plate_number, leave_time):
        sql = "UPDATE parking_record SET leave_time = '{}',state = 0 WHERE `number` = '{}' AND state = 1".format(
            leave_time, plate_number)
        print(sql)
        self.mycursor.execute(sql)
        self.mydb.commit()

    def select(self, plate_number):
        sql = "SELECT * FROM parking_record WHERE plate_number = {} AND state = 1".format(plate_number)
        self.mycursor.execute(sql)
        return self.mycursor.fetchall()

    def close(self):
        self.mydb.close()


# 进出数据处理
class moodtool(object):

    def __init__(self):
        self.mysqlmood = sqlmood()

    def in_parking(self, plate_number, colors):
        arrive_time = time.strftime('%Y-%m-%d %H:%m:%S', time.localtime(time.time()))
        self.mysqlmood.inset(plate_number, colors, arrive_time)

    def out_parking(self, plate_number):
        leave_time = time.strftime('%Y-%m-%d %H:%m:%S', time.localtime(time.time()))
        self.mysqlmood.update(plate_number, leave_time)



# 百度接口封装
class myapi(object):
    def __init__(self):
        """ 你的 APPID AK SK """
        self.APP_ID = '16145467'
        self.API_KEY = 'FiEczesyokVkyE4osO6i8Ka4'
        self.SECRET_KEY = 'N2aW8wlUKPUeat52GCsoX4n6y2x3M24t'
        self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY)
        self.mytool = moodtool()
        self.mysql = sqlmood()

    def into(self, img_path):
        image = get_file_content(img_path)

        """ 调用车牌识别 """
        result = self.client.licensePlate(image);

        """ 判断是否在库 """
        if self.mysql.datas(result['words_result']['number'], result['words_result']['color']):
            """进入记录存储"""
            self.mytool.in_parking(result['words_result']['number'], result['words_result']['color'])
            print('OK')
            dicts = {'number':result['words_result']['number'],'end':'内部车辆'}
            return (dicts)
        else:
            print('NO')
            dicts = {'number': result['words_result']['number'], 'end': '外部车辆'}
            return (dicts)
    def out(self, img_path):
        image = get_file_content(img_path)

        """ 调用车牌识别 """
        result = self.client.licensePlate(image);

        """离开记录存储"""
        self.mytool.out_parking(result['words_result']['number'])
        print('OK')


""" 读取图片 """


def filepath(img):
    filePath = 'photo' + '/' + img
    return filePath


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
